package org.Iter2.Persistencia;

import java.sql.*;

public class Agente {
	private Connection conn = null;
	private Statement stm;
	private ResultSet rs;
	
	private static Agente instancia=null;
	
	private Agente()throws Exception {
    	conectar();
    		
    }
	
    public static Agente getAgente() throws Exception{
          if (instancia==null){
        	  instancia=new Agente();
        }
        return instancia;
    }
	
	public static Agente getInstancia(){
		return instancia;
	}
	
	public void conectar(){
		try {
			Class.forName("org.gjt.mm.mysql.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost/findpersons","root", "");
			if (conn != null){
				System.out.println("Conexion a base de datos ");
				stm = conn.createStatement();
			}
		}
		catch(SQLException ex) {
			System.out.println("Hubo un problema al intentar conectarse con la base de datos");
		}
		catch(ClassNotFoundException ex) {
			System.out.println(ex);
		}
	}
	
	public ResultSet consultar(String sentencia) throws SQLException{
		rs = stm.executeQuery(sentencia);
		return rs;
	}
	
	public void gestionar(String sentencia) throws SQLException{
		stm.execute(sentencia);
	}
}
